<template>
	<view class="locus-page">
		<title text="选择时间"></title>
		<view class="start-end">
			<view class="time" v-if="isInputShow" @click="gotoSelectOperator">
				<view class="time-l">选择人员</view>
				<input class="time-ipt" type="text" placeholder="请选择人员" :disabled="true" :value="operatorInfo.operatorName" />
				<image :src="icon" mode="aspectFill"></image>
			</view>
		
			<view class="time">
				<view class="time-l">设置日期</view>
				<view class="time-r" @click="handleDate">
					{{$xeu.toDateString(dateValue, 'yyyy-MM-dd')}}
				</view>
				<image :src="icon" mode="aspectFill"></image>
			</view>
			<view class="time">
				<view class="time-l">开始时间</view>
				<view class="time-r" @click="handleStart">
					{{starttime}}
				</view>
				<image :src="icon" mode="aspectFill"></image>
			</view>
			<view class="time">
				<view class="time-l">结束时间</view>
				<view class="time-r" @click="handleEnd">
					{{endtime}}
				</view>
				<image :src="icon" mode="aspectFill"></image>
			</view>
			<u-datetime-picker
				ref="datetimePickerDate"
				:show="showPickerDate"
				v-model="dateValue"
				mode="date"
				@confirm="dateConfirm"
				@cancel="showPickerDate=false"
			></u-datetime-picker>
			
			<u-datetime-picker
				ref="datetimePickerStart"
				:show="showPickerStart"
				v-model="starttime"
				mode="time"
				@confirm="startTimeConfirm"
				@cancel="showPickerStart=false"
			></u-datetime-picker>
			<u-datetime-picker
				ref="datetimePickerEnd"
				:show="showPickerEnd"
				v-model="endtime"
				mode="time"
				@confirm="endTimeConfirm"
				@cancel="showPickerEnd=false"
			></u-datetime-picker>
		
		</view>
		<title text="快捷时间"></title>
		<view class="nav-box">
			<view 
				v-for="(item, index) in navLists" 
				:key="index" 
				:class="['nav-item', activeNav == index && 'nav-act']"
				@click="onClickNav(item,index)"
			>{{item.title}}</view>
		</view>
		
		<btnBottom text="查 询" :marginTop="48" @onBtnClick="gotoTrackDetail"></btnBottom>
	</view>
</template>

<script>
	import title from '../components/title.vue';
	import { mapState } from 'vuex';
	import btnBottom from '../components/btnBottom.vue';
	import utils from "@/utils/utils.js"
	export default{
		components: {
			title,
			btnBottom
		},
		data() {
			return {
				isInputShow: true,
				dateValue:"",
				starttime: "",
				endtime: "",
				showPickerDate: false,
				showPickerStart: false,
				showPickerEnd: false,

				pickerConfig: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: false
				},
				activeNav: -1,
				navLists: [
					{title: '1小时', value: 60, type: 'minute'},
					{title: '3小时', value: 180, type: 'minute'},
					{title: '5小时', value: 300, type: 'minute'},
					{title: '10小时', value: 600, type: 'minute'},
					{title: '今天', value: 0, type: 'day'},
					{title: '昨天', value: 1, type: 'day'},
					{title: '前两天', value: 2, type: 'day'},
					{title: '前三天', value: 3, type: 'day'}
				],
			}
		},
		onLoad(options) {
			this.isClear = options.isClear || 0
			this.init()
		},
		computed: {
			...mapState({
				operatorInfo: state => state.operator.operatorInfo
			})
		},
		onReady() {
			// 微信小程序需要用此写法
			// this.$refs.datetimePickerStart.setFormatter(this.formatter)
			// this.$refs.datetimePickerEnd.setFormatter(this.formatter)
			// this.$refs.datetimePickerDate.setFormatter(this.dateFormatter)
		},
		beforeDestroy() {
			if(this.isClear == 0) {
				this.$store.commit('SET_OPERATOR_INFO', {})
			}
		},
		methods: {
			dateFormatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			formatter(type, value) {
				if (type === 'hour') {
					return `${value}时`
				}
				if (type === 'minute') {
					return `${value}分`
				}
				if (type === 'second') {
					return `${value}`
				}
				return value
			},
			gotoSelectOperator() {
				uni.navigateTo({
					url: '/pages/operator/operatorSelect/index'
				})
			},
			
			init() {
				const defaultDate = new Date();		
				this.dateValue = uni.$xeu.timestamp(defaultDate);
				this.starttime = "00:00";
				this.endtime = uni.$xeu.toDateString(defaultDate, 'HH:mm')
			},
			
			dateConfirm(e){
				console.log("dateConfirm",e)
				this.dateValue = e.value
				this.showPickerDate = false;
				this.resetNav()
			},
			
			startTimeConfirm(e) {
				console.log("startTimeConfirm",e)
				// this.starttime = e.value
				this.showPickerStart = false;
				this.resetNav()
			},
			endTimeConfirm(e) {
				console.log(e)
				// this.endtime = e.value
				this.showPickerEnd = false;
				this.resetNav()
			},
			handleDate() {
				console.log("handleDate")
				this.showPickerDate = true;
				
			},
			handleStart() {
				console.log("handleStart")
				this.showPickerStart = true;
				
			},
			handleEnd() {
				console.log("handleEnd")
				this.showPickerEnd = true;
			},
			onClickNav(item, index){
				this.activeNav = index
				const defaultDate = new Date();	
				if (item.type == 'minute'){
					var time = uni.$xeu.timestamp(new Date()) - item.value*60*1000;			
					if (uni.$xeu.toDateString(time,'yyyy-MM-dd') != uni.$xeu.toDateString(defaultDate,'yyyy-MM-dd')){
						this.starttime = "00:00"
					}else{
						this.starttime = uni.$xeu.toDateString(time, 'HH:mm')
					}
					this.dateValue = uni.$xeu.timestamp(defaultDate);
					this.endtime = uni.$xeu.toDateString(defaultDate, 'HH:mm')
				}else if (item.type == 'day'){
					var time = uni.$xeu.getWhatDay(new Date(), -item.value)
					this.dateValue = uni.$xeu.timestamp(time);
					this.starttime = "00:00"
					this.endtime = "23:59"
				}
			},
			resetNav(){
				this.activeNav = -1
			},
			gotoTrackDetail(){
				
				if (this.checkParams()){
					var dateValue = uni.$xeu.toDateString(this.dateValue,'yyyy-MM-dd')
					var startDatetime = dateValue  + " " + this.starttime + ":00"
					var endDatetime = dateValue + " " + this.endtime + ":00"
				
					var obj = {
						startTime:  startDatetime,
						endTime: endDatetime,
						operatorId: this.operatorInfo.id,
						operatorName: this.operatorInfo.operatorName,
					}
					console.log("gotoTrackDetail", utils.KeyValueToUrl(obj))	
					uni.navigateTo({
						url: '/pages/operator/trackDetail/index?' + utils.KeyValueToUrl(obj)
					})
					
				}
			},
			
			checkParams(){
				console.log("checkParams",this.operatorInfo)
				if (!(this.operatorInfo&&this.operatorInfo.id)){
					uni.$snowy.modal.msgError("请选择人员")
					return false
				}
		
				if (!this.dateValue){
					uni.$snowy.modal.msgError("请选择设置日期")
					return false
				}
				if (!this.starttime){
					uni.$snowy.modal.msgError("请选择开始时间")
					return false
				}
				if (!this.endtime){
					uni.$snowy.modal.msgError("请选择结束时间")
					return false
				}
				var dateValue = uni.$xeu.toDateString(this.dateValue,'yyyy-MM-dd')
				var startDatetime = dateValue  + " " + this.starttime 
				var endDatetime = dateValue + " " + this.endtime 
				if (uni.$xeu.timestamp(startDatetime) >= uni.$xeu.timestamp(endDatetime)){
					uni.$snowy.modal.msgError("开始时间必须小于结束时间")
					return false
				} 
				return true	
			}
		}
	}
</script>

<style lang="scss" scoped>
	.locus-page{
		padding: 0 24rpx;
		min-height: 100vh;
		background: #F9F9FA;
	}
	
	.start-end{
		padding: 0 24rpx;
		background: #FFFFFF;
		border: 1px solid #E5E5E6;
		border-radius: 8rpx;
	}
	.time{
		height: 99rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
	}
	.time image{
		width: 50rpx;
		height: 50rpx;
	}
	.time-l{
		height: 30rpx;
		padding-right: 36rpx;
		border-right: 1px solid #E4E4E4;
		margin-right: 36rpx;
	}
	.time-r, .time-ipt{
		flex: 1;
		color: #666666;
		min-width: 350rpx;
	}
	
	.nav-box{
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		.nav-item{
			width: 166rpx;
			height: 68rpx;
			background: #FFFFFF;
			border: 1px solid #E5E5E6;
			border-radius: 8rpx;
			color: #333333;
			font-size: 28rpx;
			text-align: center;
			line-height: 68rpx;
			margin-bottom: 10rpx;
		}
		.nav-act{
			color: #fff;
			background: #3791E9;
		}
	}
	
</style>
